function initPlan() {
    $("body").on('click', '.attention_plan_bt li', function () {
        $(this).addClass("current").siblings().removeClass("current");
        var para = $(this).attr('data-type');
        var html = '';
        $.ajax({
            url: 'allPlan/para/' + para,
            type: 'get',
            success: function (res) {
                console.log(res)
//                    html += '<ul class="new_plan_lists">';
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        html += '<li><a href="show/id/' + res[i].plan_id + '"><span class="plan_nr">' + (i + 1) + '.' + res[i].name + '</span><span class="plan_att_num">50%</span></a></li>';
                    }
                } else {
                    html += '<li><a href="#"><span class="plan_nr">暂无数据</span></a></li></ul>'
                }
//                    html += '</ul>'; 
                $('.new_plan_lists').html(html);
            }
        })
    });
    $("body").on('click', '.idTabs li', function () {
        $(this).addClass("allday").siblings().removeClass("allday");
        var dept_para = $(this).attr('data-id');
        var html = '';
        var htmlHeader = '';
        if (!$.isNumeric(dept_para)) {
            history.go(0);
        } else {
            $.ajax({
                url: 'deptPlan/para/' + dept_para,
                type: 'get',
                success: function (res) {
                    console.log(res)
//                        html += '<ul class="new_plan_lists">';
                    if (res.weekPlan.length > 0) {
                        for (var i = 0; i < res.weekPlan.length; i++) {
                            html += '<li><a href="show/id/' + res.weekPlan[i].plan_id + '"><span class="plan_nr">' + (i + 1) + '.' + res.weekPlan[i].name + '</span><span class="plan_att_num">50%</span></a></li>';
                        }
                    } else {
                        html += '<li><a href="#"><span class="plan_nr">暂无数据</span></a></li></ul>'
                    }
//                        html += '</ul>'; 
                    $('.new_plan_lists').html(html);

                    htmlHeader += '<li class="current" data-type="1"><a class="pointer"><span>' + res.count.weekCount + '</span><br/>周计划</a></li><li data-type="2"><a class="pointer"><span>' + res.count.monthCount + '</span><br/>月计划</a></li><li data-type="11"><a class="pointer"><span>' + res.count.projectCount + '</span><br/>项目计划</a></li><li data-type="7"><a class="pointer"><span>' + res.count.yearCount + '</span><br/>年计划</a></li>';
                    $('.attention_plan_bt').html(htmlHeader);
                }
            })
        }
    })
}

function initAddPlan() {
    //判断计划类型
    $('select[name=type]').on('change', function () {
        if ($(this).val() != 1) {
            $('.complex').show();
            $('.simple').hide();
        } else {
            $('.complex').hide();
            $('.simple').show();
        }
    })

    //展开子分类
    $('.open').on('click', function () {
        if ($('.childCate').css('display') == 'none') {
            $('.childCate').show();
            $(this).children('label').text('收起子分类');
            $(this).children('img').attr('src', '/Public/Admin/images/arrow-up.png')
        } else {
            $('.childCate').hide();
            $(this).children('label').text('展开子分类');
            $(this).children('img').attr('src', '/Public/Admin/images/arrow-down.png')
        }
    })

    $('.input_lable').on('click', function () {
        $(this).children('i').addClass('radio_bg_check');
        $(this).siblings().children('i').removeClass('radio_bg_check');
        $(this).children().find('input').attr('checked', true);
        $(this).siblings().children().find('input').attr('checked', false);
    })

    //选择人员
    $(document).on('click', '.addUser', function () {
        var id = $(this).attr('data-id');
        var key = $(this).attr('data-key');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '550';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/1/id/" + id + "/key/" + key
        })
    })
    //选择角色
    $('.addPriv').on('click', function () {
        var id = $(this).attr('data-id');
        var key = $(this).attr('data-key');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '300';
        var layer_h = '500';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择角色",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/7/id/" + id + "/key/" + key
        })
    })
    //清空人员或部门
    $('.delData').on('click', function () {
        $(this).parent('.value').find('.textarea span:not(:first)').remove();
    })

    //删除单个用户
    $('body').on('click', '.delOperator', function () {
        $(this).parent('span').remove();
    })

    //选择部门
    $(document).on('click', '.addDept', function () {
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '300';
        var layer_h = '500';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择部门",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/3"
        })
    })

    $(function () {
        var url = $('.getChildType').val() + '?id=' + $('select[name=type]').val();
        getChildType(url);
    })

    $('select[name=type]').on('change', function () {
        var id = $(this).val();
        var url = $('.getChildType').val() + '?id=' + id;
        getChildType(url);
    })

    function getChildType(url) {
        var type = $('.currType').val();
        $.get(url, function (res) {
            console.log(res);
            var html = '<option value="0">请选择</option>';
            if (res) {
                for (var i = 0; i < res.length; i++) {
                    html += '<option value="' + res[i].type_id + '"';
                    if (type == res[i].type_id) {
                        html += 'selected';
                    }
                    html += '>' + res[i].type_name + '</option>';
                }
            } else {
                html += '<option value="">无</option>';
            }
            $('select[name=child_type]').html(html);
        })
    }
}

//进度日志
function initSchedule() {
    $('.is_diary').on('click', function () {
        if ($(this).attr('src') == '/Public/Index/img/s2.png') {
            $(this).attr('src', '/Public/Index/img/s1.png').siblings().attr('src', '/Public/Index/img/s2.png');
            if ($(this).index() == 0) {
                $('input[name=is_diary]').val(1);
            } else {
                $('input[name=is_diary]').val(0);
            }
        } else {
            $(this).attr('src', '/Public/Index/img/s2.png');
            if ($(this).index() == 0) {
                $('input[name=is_diary]').val(0);
            }
        }
    })

    //批注弹窗
    $('body').on('click', '.comment', function () {
        var id = $(this).attr('data-id');
        var is_end = $(this).attr('data-type');
        var dept = $(this).attr('data-dept');
        var cType = $(this).attr('data-cType');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '600';
        var layer_h = '500';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "批注内容",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/5/id/" + id,
        })
    })

    //计划查询
    $('.search').on('click', function () {
        var url = $(this).attr('data-url');
        var data = $('form').serialize();
        $.post(url, data, function (res) {
            console.log(res);
            var html = '';
            if (res.lists.length > 0) {
                for (var i = 0; i < res.lists.length; i++) {
                    html += '<tr><td width="60%"><a href="/index.php/Home/Plan/show/id/' + res.lists[i].plan_id + '">' + res.lists[i].name + '</a></td>';
                    html += '<td>' + res.lists[i].status + '</a></td>';
                    html += '<td>';
                    if (res.lists[i].sms_flag == 0) {
                        html += '<a href="/index.php/Home/Plan/schedule/id/' + res.lists[i].plan_id + '">写进度</a><br/>';
                    }
                    if (res.lists[i].publish == 0) {
                        html += '<a class="pointer">提交</a>';
                    } else {
                        html += '<a class="comment pointer" data-id="' + res.lists[i].plan_id + '">批注</a>';
                    }
                    html += '</td></tr>';
                }

            } else {
                html += '<tr><td colspan="5" style="text-align: left">暂无数据</td></tr>'
            }
            $('.transferred_table table tbody').html(html);
            $('#total').text(res.total)
        })
    })

    //计划查询输入框重置
    $('.restart').on('click', function () {
        $('.work_cells table input').val('');
        $('.work_cells table select').val(0);
    })

    //增加百分比
    $('.value .add').on('click', function () {
        var num = parseInt($('.p-value').text());
        num += 5;
        if (num > 100) {
            num = 100;
        }
        $('.p-value').text(num);
        $('input[name=percent]').val(num);
    })

    //减少百分比
    $('.value .dec').on('click', function () {
        var oldNum = $('input[name=old_total]').val();
        var num = parseInt($('.p-value').text());
        num -= 5
        if (num < oldNum) {
            num = oldNum;
        }
        $('.p-value').text(num);
        $('input[name=percent]').val(num);
    })

    //删除批注或进度
    $('.delDetail').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定删除该条记录吗？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                if (res.status == 1) {
                    layer.msg('删除成功');
                    location.href = res.url;
                } else {
                    layer.msg('删除失败');
                }
            })
        }, function (index) {
            layer.close(index);
        })
    })

    //显示附件
    $('.cd-timeline-block').on('click', '.attach', function (e) {
        _this = $(this);
        $(this).next().slideDown();

        $(document).on('click', function () {
            $(_this).next().slideUp();
        })
        e.stopPropagation();
    })

    $('.attach_lists').on('click', function (e) {
        e.stopPropagation();
    })

    //点击放大图片
    $('.showImg').on('click', function () {
        var path = $(this).attr('src');
        var d_w = $('.layui-larry-box').width();
        var d_h = $('.layui-larry-box').height();

        if ($(this).width() > $(this).height()) {
            var img_w = $(this).width() * 20;
            var img_h = $(this).height() * 20;
        } else {
            var img_w = $(this).width() * 15;
            var img_h = $(this).height() * 15;
        }

        var close_w = (img_w - 30) + 'px';
        var rotate_w = (img_w - 60) + 'px';

        var w = (d_w / 2 - img_w / 2) + 'px';
        if (img_h > d_h) {
            var h = 0;
        } else {
            var h = ((d_h - img_h) / 2) + 'px';
        }

        $('.bigImg').css({width: img_w, height: img_h, left: w, top: h});
        var html = '<img src="' + path + '" style="width:100%;position:relative" />';
        html += '<img class="pointer closeImg" src="/Public/Index/img/close_img.png" style="position: absolute;top:8px;left:' + close_w + '" />';
        html += '<img class="pointer rotateImg" src="/Public/Index/img/rotate.png" style="position: absolute;top:8px;left:' + rotate_w + '" />';
        $('.bigImg').html(html);
        $('.bigImg').show();
    })

    //关闭图片
    $('body').on('click', '.closeImg', function () {
        $('.bigImg').hide();
    })

    //旋转图片
    var rot = 0;
    $('body').on('click', '.rotateImg', function () {
        //$('.bigImg img').css({"transform":"rotate(90deg)"});
        if (rot === 360) {
            rot = 0;
        }
        rot += 90;
        $('.bigImg img').rotate(rot);
        var img_h = $('.bigImg img').height();
        var close_w = (img_h + 60) + 'px';
        var rotate_w = (img_h + 30) + 'px';
        $('.closeImg').css({left: close_w, top: '5px'});
        $('.rotateImg').css({left: rotate_w, top: '5px'});
    })

    //收放批注
    $(document).on("click", ".notView .showOrNot", function () {
        if ($(this).attr("state") == "show") {
            $(this).attr("state", "hide");
            $(this).find("img").attr("src", "/Public/Admin/images/arrow-down.png");
            $(this).parent().addClass("notViewWrap-hide");
        } else if ($(this).attr("state") == "hide") {
            $(this).attr("state", "show");
            $(this).find("img").attr("src", "/Public/Admin/images/arrow-up.png");
            $(this).parent().removeClass("notViewWrap-hide");
        }
    });

}

function initPlan() {
    $('.myPlan').on('click', function () {
        if ($('.weui_cell_ul').css('display') == 'none') {
            $('.weui_cell_ul').slideDown();
        } else {
            $('.weui_cell_ul').slideUp();
        }
    })
}

function initPlanIndex() {
    $('.delete').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定要删除该计划吗?', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.get(url, function (res) {
                console.log(res);
                if (res.status == 1) {
                    layer.msg('删除成功');
                    location.reload();
                } else {
                    layer.msg('删除失败');
                }
            })
        })
    })

    $('.theend').on('click', function () {
        var url = $(this).attr('data-url');
        var type = $(this).attr('data-type');
        var str = '';
        if (type == 1) {
            str = '确定强制结束该计划吗?';
        } else {
            str = '确定强制开始该计划吗?';
        }
        layer.confirm(str, {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                if (res == 1) {
                    layer.msg('操作成功');
                    location.reload();
                } else {
                    layer.msg('操作失败');
                }
            })
        })
    })

    //加关注
    $('body').on('click', '.follow', function () {
        var plan_id = $(this).attr('data-id');
        var url = $('#followUrl').val() + '?plan_id=' + plan_id;
        $.get(url, function (res) {
            console.log(res);
            layer.msg(res.msg);
            setTimeout("location.reload()", 2000);
        })
    })


    $('.search').click(function () {
        $('form').submit();
    })

    $('.remind').on('click', function () {
        var _this = $(this);
        var url = $(this).attr('data-url');
        $.get(url, function (res) {
            console.log(res)
            if (res.status) {
                layer.msg(res.msg);
                var title = '上次提醒时间：' + res.remind;
                _this.attr('title', title);
                _this.children('img').attr('src', '/Public/Admin/images/remind.png');
            }
        })
    })

    //跳转
    $('.paging').on('click', function () {
        $('input[name=p]').remove();
        var data = $('form').serialize();
        var url = $(this).attr('data-url') + '?p=' + $('input[name=number]').val();
        location.href = url + data;
    })


}
